package com.situ.mall.base.util;

import java.text.DecimalFormat;
import java.util.Objects;

/**
 * ClassName: FileUtil
 * Package: com.situ.mall.base.util
 * Description:
 *
 * @Author Darling_达
 * @Create 2023-03-22 19:24
 * @Version 1.0
 */

public class FileUtil {

    //文件大小转kb
    public static String formatFileSize(Long fileSize) {
        String fileSizeStr = "";
        if (Objects.isNull(fileSize)) {
            return fileSizeStr;
        }
        if (fileSize == 0L) {
            return "0".concat(C.B);
        }
        // 小数位数自定义
        DecimalFormat df = new DecimalFormat("#.##");
        if (fileSize < C.K_SIZE) {
            fileSizeStr = df.format((double) fileSize) + C.B;
        } else if (fileSize < C.M_SIZE) {
            fileSizeStr = df.format((double) fileSize / C.K_SIZE) + C.K;
        } else if (fileSize < C.G_SIZE) {
            fileSizeStr = df.format((double) fileSize / C.M_SIZE) + C.M;
        } else {
            fileSizeStr = df.format((double) fileSize / C.G_SIZE) + C.G;
        }
        return fileSizeStr;
    }

}
